﻿using System;
using personalplaner.common.controls;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows;
using System.Threading;
using personalplaner.common.threading;

namespace personalplaner.common.utilities.controls
{
    /// <summary>
    /// creates a semi transparent background that overlays the gui when the witindicator is showing
    /// </summary>
    public class LoadingBackground : VisualWrapper
    {
        public LoadingBackground()
        {
            SizeChanged += new SizeChangedEventHandler(ThreadedProgressBar_SizeChanged);
            Child = CreateProgressBarOnWorkerThread();
        }

        /// 
        /// border
        /// 
        Border _border;

        private static AutoResetEvent s_event = new AutoResetEvent(false);

        private HostVisual CreateProgressBarOnWorkerThread()
        {
            // Create the HostVisual that will "contain" the VisualTarget
            // on the worker thread.
            HostVisual hostVisual = new HostVisual();

            // Spin up a worker thread, and pass it the HostVisual that it
            // should be part of.
            Thread thread = new Thread(new ParameterizedThreadStart(MediaWorkerThread));
            thread.SetApartmentState(ApartmentState.STA);
            thread.IsBackground = true;
            thread.Start(hostVisual);

            // Wait for the worker thread to spin up and create the VisualTarget.
            s_event.WaitOne();

            return hostVisual;
        }

        private void MediaWorkerThread(object arg)
        {
            // Create the VisualTargetPresentationSource and then signal the
            // calling thread, so that it can continue without waiting for us.
            var hostVisual = (HostVisual)arg;
            var visualTargetPS = new VisualTargetPresentationSource(hostVisual);
            s_event.Set();

            // Create a MediaElement and use it as the root visual for the VisualTarget.
            _border = new Border();
            _border.Background = Brushes.Black;
            _border.Opacity = 0.5;

            _border.Width = 3000;
            _border.Height = 2000;

            _border.UpdateLayout();

            visualTargetPS.RootVisual = _border;

            // Run a dispatcher for this worker thread.  This is the central
            // processing loop for WPF.
            System.Windows.Threading.Dispatcher.Run();
        }

        private void ThreadedProgressBar_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            //border.Dispatcher.Invoke(new Action(delegate
            //{
            //    border.Width = e.NewSize.Width;
            //    border.Height = e.NewSize.Height;
            //}));
        }
        

        public void SetVisibility(Visibility visibility)
        {
            _border.Dispatcher.Invoke(new Action(delegate
            {
                _border.Visibility = visibility;
            }));
        }

        
    }
}
